<?php
/*
 * 系列
 */
class CollectionAction extends AdminAction{
    
    public function index(){
        $collection_db = M('collection');
        $list = $collection_db->where('language_id=6')->order('collection_id')->select();
        $this->assign('list',$list);
        $this->display();
    }
    
    public function add(){

     //得到语言列表
    $lang_db = D('language');
        $languageArray = $lang_db->field('id,name,directory')->order('listorder DESC')->select();
        if($languageArray){
                foreach ($languageArray as $lang){
                        $languageList[$lang['id']] = $lang;
                }
        }
        if(IS_POST){//var_dump($_FILES['imgs']['name']);die;
            //var_dump($_FILES);die;
            $collection_db = M('collection');
            $maxid = $collection_db->max('collection_id');//当前数据库最大系列id +1 作为当前系列id

            foreach($_POST['des'] as $k=>$v){
//                if(empty($v)){
//                    continue;
//                }
                $collection['url_path'] = $_POST['path'];
                $collection['language_id'] = $k;
                $collection['collection_title'] = $v;
                $collection['meta_title']       = $_POST['meta_title'][$k];
                $collection['meta_keywords']    = $_POST['meta_keywords'][$k];
                $collection['meta_description'] = $_POST['meta_description'][$k];
                $collection['collection_id'] = $maxid+1;
               // $collection['is_display']  = $_POST['is_display'];
                $collection_db->add($collection); //首先更新collection表
            }
                $len = count($languageList);
                 foreach($_POST['banner'] as $k=>$v){
                    for($j=$k*$len;$j<($k+1)*$len;$j++){
                    $data = array();
                    $FILE = array();
                           foreach($languageList as $k2=>$v2){//将图片数组格式 转换成可上传格式，并把需要更新到数据库的数据保存
                               if(isset($_FILES['imgs']['tmp_name'][$j][$k2])){
                                   $FILE['name'] = $_FILES['imgs']['name'][$j][$k2];
                                   $FILE['type'] = $_FILES['imgs']['type'][$j][$k2];
                                   $FILE['tmp_name'] = $_FILES['imgs']['tmp_name'][$j][$k2];
                                   $FILE['error'] = $_FILES['imgs']['error'][$j][$k2];
                                   $FILE['size'] = $_FILES['imgs']['size'][$j][$k2];
                                   $data['pids'] = $_POST['pid'][$k];//产品ids
                                   $data['img_language_id'] = $k2;//语言id
                                   $data['banner_id'] = $k; 
                                   $data['collection_id'] = $maxid+1;    //系列id
                                   $datas[] = $data;//数据数组
                                   $arr[] = $FILE;//图片数组
                                   break;
                               }
                           }  
                        }
                     }  
            unset($_FILES);
            
            $_FILES = $arr;          
            $info = $this->upload(date('Y/m/d', time()));//把所有图片上传  

            $collection_items_db = M('collection_items');
            foreach($datas as $k=>$v){//更新collection_items表
                $datas[$k]['img_path'] = trim($info[$k]['savepath'].$info[$k]['savename'],'.'); //处理出入数据库的图片路径地址
                $collection_items_db->add($datas[$k]);
            }
            $this->redirect('collection/index');
            exit;
         }
        $this->assign('languageList',$languageList);
        $this->display();
    }
    
    public function edit($id){
    //得到语言列表
    $lang_db = D('language');
        $languageArray = $lang_db->field('id,name,directory')->order('listorder DESC')->select();
        if($languageArray){
                foreach ($languageArray as $lang){
                        $languageList[$lang['id']] = $lang;
                }
        }
        
        if(IS_POST){
            $collection_db = M('collection');
            foreach($_POST['des'] as $k=>$v){
                $where['language_id'] = $k;
                $where['collection_id'] = $_POST['collection_id'];
                $collection['url_path'] = $_POST['path'];
                $collection['collection_title'] = $v;
                $collection['meta_title']       = $_POST['meta_title'][$k];
                $collection['meta_keywords']    = $_POST['meta_keywords'][$k];
                $collection['meta_description'] = $_POST['meta_description'][$k];            
                //$collection['is_display'] = $_POST['is_display'];
                $collection_db->where($where)->save($collection);
            }
             $len = count($languageList);
            foreach($_POST['banner'] as $k=>$v){    
                for($j=$k*$len;$j<($k+1)*$len;$j++){
               $data = array();
               $FILE = array();
                   foreach($languageList as $k2=>$v2){
                       if(isset($_FILES['imgs']['tmp_name'][$j][$k2])){
                         $FILE['name'] = $_FILES['imgs']['name'][$j][$k2];
                         $FILE['type'] = $_FILES['imgs']['type'][$j][$k2];
                         $FILE['tmp_name'] = $_FILES['imgs']['tmp_name'][$j][$k2];
                         $FILE['error'] = $_FILES['imgs']['error'][$j][$k2];
                         $FILE['size'] = $_FILES['imgs']['size'][$j][$k2];
                         $data['pids'] = $_POST['pid'][$k];
                         $data['img_language_id'] = $k2;
                         $data['banner_id'] = $k; 
                         $data['collection_id'] = $_POST['collection_id'];    
                         $datas[] = $data;//banner列表信息数据
                         $arr[] = $FILE;//图片数据
                         break;
                       }
                   }  
               }
            }  

            unset($_FILES);
            $_FILES = $arr;  //把所有图片上传          
            $info = $this->upload(date('Y/m/d', time()));
            $collection_items_db = M('collection_items');
            //得到最大banner_id在后面判断是否有新增的banner
            $m_banner_id = $collection_items_db->field('banner_id')->where('img_language_id=1 and collection_id='.$_POST['collection_id'])->order('banner_id desc')->find();
//            echo '<pre>';
//            var_dump($datas);die;
            foreach($datas as $k=>$v){
                if($v['banner_id']>$m_banner_id['banner_id']){//如果是新增的banner
                    if($datas[$k]['pids'] == ''){//如果没有写产品id 就不保存
                        break;
                    }
                    $datas[$k]['img_path'] = trim($info[$k]['savepath'].$info[$k]['savename'],'.');//处理图片路径
                    $collection_items_db->add($datas[$k]);   //添加到数据库
                }else{//修改原来的banner
                    $data = array();
                    $where = array();
                    if(isset($info[$k]['savepath'])){//如果上传了图片，就更新数据库内图片信息
                        $data['img_path'] = trim($info[$k]['savepath'].$info[$k]['savename'],'.');
                    }
                    $data['pids']= $datas[$k]['pids'];
                    $where['banner_id'] = $datas[$k]['banner_id'];
                    $where['img_language_id'] = $datas[$k]['img_language_id'];
                    $where['collection_id'] = $datas[$k]['collection_id'];
                    $collection_items_db->where($where)->save($data);
                }
            }
            $this->redirect('collection/index');
            exit;
        }

        $collection_db = M('collection');
        $c_info = $collection_db->where('collection_id='.$id)->select();
        $info= array();
        foreach($c_info as $k=>$v){
            $info[$v['language_id']] = $v;//专题描述
        }  
        $collection_items = M('collection_items');
        //有几个banner
        $banner = $collection_items->field('banner_id')->where('collection_id='.$id.' and img_language_id=1')->order('banner_id')->select();
        foreach($banner as $k=>$v){
            $banner_info[$v['banner_id']] = $collection_items->where('collection_id='.$id.' and banner_id='.$v['banner_id'])->select();//图片和其他信息
        }
        
        $this->assign('collection_id',$id);
        $this->assign('info',$info);
        $this->assign('languageList',$languageList);
        $this->assign('banner_info',$banner_info);
        $this->display();
    }
    
    public function del($id){
        M('collection')->where('collection_id='.$id)->delete();
        M('collection_items')->where('collection_id='.$id)->delete();
        $this->redirect('collection/index');
    }
 
}